import { IObject, udp } from '@cscec/udp-ui';
import { ITable } from '@/components/editor/plugin';
import { encodeParams } from '../common';

/**
 * 获取表列元数据信息
 */
export async function getTableFields() {
  const bizCode = udp.getQueryValue('bizCode') || udp.getQueryValue('busType');
  if (!bizCode) { return []; }
  const { code, message, data } = await udp.request.get({
    url: '/engine/metadata/dataModelDesign/queryBizTableListByBizCode',
    data: { bizCode, extendFlg: 1 },
  });
  if (code === 0) {
    return data || [];
  }
  udp.alert(message);
  return [];
}

/**
 * 根据url的id获取设计器数据
 */
export async function getDesignContent(id?: string) {
  id = id || udp.getQueryValue('id');
  if (!id) { return {}; }
  const { code, message, data } = await udp.request.get({
    url: '/engine/metadata/uiExtendSchemeDesign/getCustomFormUiSchemeInfoById',
    data: { id },
  });
  if (code === 0) {
    return data ? {
      listInfo: udp.parseJson(data.listUiContent),
      editInfo: udp.parseJson(data.editUiContent),
      jsInfo: data.scriptPublish,
      id,
    } : {};
  }
  udp.alert(message);
  return {};
}

// 保存设计器内容
export async function saveDesign(params: IDesignParams) {
  const { code, message } = await udp.request.body({
    url: '/engine/metadata/uiExtendSchemeDesign/newSaveCustomFormUiExtendSchemeDesignById',
    // url: '/engine/metadata/uiExtendSchemeDesign/saveCustomFormUiExtendSchemeDesignById',
    data: encodeParams(params),
  });
  if (code === 0) {
    udp.message('保存成功！', 'success');
    return true;
  }
  udp.alert(message);
  return false;
}

interface IDesignParams {
  id: string;
  bizCode: string;
  listUiContent: object;
  editUiContent: object;
  scriptContent: string;
}
